class CoursesController < ApplicationController

	before_filter :check_user_type, :only => [:semester_courses_name]
	
	def check_user_type
		@_current_user ||= session[:user_id] &&  User.find(session[:user_id]) 
		if @_current_user.user_type == "student"
			redirect_to "/"
			#scotty: you now need to call this method
		else 
			redirect_to "/"
		end
	end
	
	def login 
		if @user = User.authenticate(params[:email], params[password])
			reset_session
			session[:user_id] = @user_id
			redirect_to home_url
		end
	end
	
	def logout
		reset_session
		redirect_to "/login"
	end

	def semester_courses_name
		@semester = Semester.find_by_term(params[:semester_term])
		#@semester = Semester.find(params[:id])
		render :partial=>"course_list", :locals=>{:courses=>@semester.courses}
	end
	
	def show_name
    @course = Course.find(:first, params[:semester_term])

		respond_to do |format|
		  format.html # show.html.erb
		  format.xml  { render :xml => @course }
		end
	end
		
  
  # GET /courses
  # GET /courses.xml
  def index
    @courses = Course.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @courses }
    end
  end

  # GET /courses/1
  # GET /courses/1.xml
  def show
    #@course = Course.find(params[:id])
	
	#@semesters = Semester.find_by_term(params[:semester_term])
	#@course = @semesters.find(:all, :conditions => ["name = ?", params[:course_name]])
	#@course = @semester.find_by_name(params[:course_name])
	
	@course = Course.find(:first, :conditions => ["term = ? and name = ?", params[:semester_term], params[:course_name]])
	#@course = Course.find(:all, :conditions => ["semester_term = ? and name = ?", params[:semester_term], params[:course_name]])


    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @course }
    end
  end

  # GET /courses/new
  # GET /courses/new.xml
  def new
    @course = Course.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @course }
    end
  end

  # GET /courses/1/edit
  def edit
    #@course = Course.find(params[:id])
	@course = Course.find(:first, :conditions => ["term = ? and name = ?", params[:semester_term], params[:course_name]])

  end

  # POST /courses
  # POST /courses.xml
  def create
    @course = Course.new(params[:course])

    respond_to do |format|
      if @course.save
        flash[:notice] = 'Course was successfully created.'
        #format.html { redirect_to(@course) }
        format.html { render :action => "show" }
        format.xml  { render :xml => @course, :status => :created, :location => @course }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @course.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /courses/1
  # PUT /courses/1.xml
  def update
    @course = Course.find(params[:id])

    respond_to do |format|
      if @course.update_attributes(params[:course])
        flash[:notice] = 'Course was successfully updated.'
        format.html { render :action => "show" }
		#format.html { redirect_to(@course) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @course.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /courses/1
  # DELETE /courses/1.xml
  def destroy
    @course = Course.find(params[:id])
    @course.destroy

    respond_to do |format|
      format.html { render :action => "show" }
	  #format.html { redirect_to(courses_url) }
      format.xml  { head :ok }
    end
  end
end
